package com.yinggu.servlet;

import com.yinggu.pojo.KDept;
import com.yinggu.pojo.KUser;
import com.yinggu.service.DeptService;
import com.yinggu.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userEdit")
public class UserEdit extends HttpServlet {
    UserService service = new UserService();
    DeptService deptService = new DeptService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        final String id = req.getParameter("id");
        KUser user = service.getUser(Integer.parseInt(id));
        req.setAttribute("user", user);
        List<KDept> depts = deptService.getDepts();
        req.setAttribute("depts", depts);
        req.getRequestDispatcher("/admin/userEdit.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        final String userName = req.getParameter("userName");
        final String userNo = req.getParameter("userNo");
        final String deptNo = req.getParameter("deptNo");
        final String sex = req.getParameter("sex");
        final int age = Integer.parseInt(req.getParameter("age").toString());
        KUser user = new KUser();
        user.setId(Integer.parseInt(id));
        user.setUserNo(userNo);
        user.setUserName(userName);
        user.setDeptNo(deptNo);
        user.setSex(sex);
        user.setAge(age);

        service.editUser(user);
        resp.sendRedirect(req.getContextPath()+"/userList");
    }
}
